package com.novax.ex.activity.provider.service;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.provider.api.model.BeebUserResponse;
import com.novax.ex.activity.provider.common.BinlogUtils;
import com.novax.ex.activity.provider.handler.provider.BeebUserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * @Description
 * @Author zhaolei
 * @Date 10/24/24 2:47 PM
 * @Version 1.0
 */
@Slf4j
@Service
public class UserBigdataService {

    public static Set<String> binLogTypeSet = new HashSet<>(Arrays.asList("INSERT"));
    public static String tableName = "user";

    @Resource
    private BeebUserProvider beebUserProvider;

    public void execBigData(String msg) {
        List<LinkedHashMap<String, String>> data = BinlogUtils.getData(msg, binLogTypeSet, tableName);
        if (Objects.nonNull(data)) {
            for (LinkedHashMap<String, String> datum : data) {
                /**
                 * `id` int(11) NOT NULL AUTO_INCREMENT,
                 `area_code` varchar(8) DEFAULT '' COMMENT '区号',
                 `phone` varchar(16) DEFAULT NULL COMMENT '手机号',
                 `email` varchar(64) DEFAULT NULL COMMENT '邮箱',
                 **/
                String id = datum.get("id");
                String area_code = datum.get("area_code");
                String phone = datum.get("phone");
                String email = datum.get("email");
                //log.info(id+"--"+area_code+"--"+phone+"--"+email);
                BeebUserResponse userResponse = new BeebUserResponse();
                userResponse.setId(Long.parseLong(id));
                userResponse.setAreaCode(area_code==null?null:area_code);
                userResponse.setPhone(phone==null?null:phone);
                userResponse.setEmail(email==null?null:email);
                beebUserProvider.sendMsg(userResponse.getId().toString(), JSON.toJSONString(userResponse));
            }
        }
    }
}
